package com.meida.module.product.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.meida.common.constants.CommonConstants;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ListModel;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.ApiAssert;
import com.meida.module.product.client.entity.ProdBrand;
import com.meida.module.product.client.entity.ProdCategory;
import com.meida.module.product.provider.mapper.ProdBrandMapper;
import com.meida.module.product.provider.service.ProdBrandService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 品牌 实现类
 *
 * @author flyme
 * @date 2019-07-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProdBrandServiceImpl extends BaseServiceImpl<ProdBrandMapper, ProdBrand> implements ProdBrandService {

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody listEntityMap(ListModel model) {
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(model, ProdBrand.class);
        cq.select(ProdBrand.class, "brandId", "brandName", "brandLogo");
        cq.eq("categoryId");
        return baseList(cq);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody pageList(Map map) {
        CriteriaQuery<ProdBrand> cq = new CriteriaQuery(map, ProdBrand.class);
        PageParams pageParams = cq.getPageParams();
        ProdBrand query = cq.getEntity(ProdBrand.class);
        cq.select(ProdBrand.class, "brandId", "brandName", "brandLogo", "categoryId", "recommend");
        cq.select(ProdCategory.class, "categoryName");
        cq.eq(ProdBrand.class, "categoryId");
        cq.likeByField(ProdBrand.class, "brandName");
        cq.createJoin(ProdCategory.class);
        return basePageList(cq);
    }


    @Override
    public ResultBody setRecommend(Long[] brandIds, Integer recommend) {
        ApiAssert.isNotEmpty("主键不能为空", brandIds);
        UpdateWrapper product = new UpdateWrapper();
        product.set("recommend", recommend);
        product.in("brandId", brandIds);
        boolean result = update(product);
        if (result) {
            return ResultBody.ok("操作成功", recommend);
        } else {
            return ResultBody.failed("操作失败");
        }
    }


    @Override
    public List<EntityMap> selectByCategoryId(Long categoryId) {
        CriteriaQuery cq = new CriteriaQuery(ProdBrand.class);
        cq.eq("categoryId", categoryId);
        cq.eq("recommend", CommonConstants.ENABLED);
        return selectEntityMap(cq);
    }

}
